package com.mes.inventory.dao;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import com.bstek.dorado.data.entity.EntityUtils;
import com.bstek.dorado.data.provider.Page;
import com.mes.common.dao.BasicDao;
import com.mes.inventory.dao.iface.IInvIODetailDao;
import com.mes.inventory.domain.InvIO;
import com.mes.inventory.domain.InvIODetail;

@Repository
public class InvIODetailDao extends BasicDao<InvIODetail> implements IInvIODetailDao{

	@Override
	public void saveInvIODetail(Set<InvIODetail> invIODetails) throws Exception {
		for (InvIODetail i : invIODetails) {
			switch (EntityUtils.getState(i)) {
			case MODIFIED:
				this.update(i);
				break;
			case DELETED:
				this.delete(i);
				break;
			case NEW:
				this.add(i);
				break;
			default:
				break;
			}
		}
	}

	@Override
	public void getInvIODetailByCondition(Page<InvIODetail> page, Map<String, Object> params) throws Exception {
		// TODO Auto-generated method stub
		Map<String, Object> args = new HashMap<String, Object>();
		StringBuffer coreHql = new StringBuffer("from "
				+ InvIODetail.class.getName() + " a where 1=1 ");
		
		Date beginDate =  (Date) params.get("beginDate");
		Date endDate =  (Date) params.get("endDate");
		
		if (null != beginDate && null != endDate) {
			coreHql.append(" and a.createTime between :beginDate and :endDate ");
			args.put("beginDate", beginDate);
			args.put("endDate", endDate);
		}
		
		
		String partDrawingId = (String) params.get("partDrawingId");
		if (StringUtils.isNotEmpty(partDrawingId)) {
			coreHql.append(" and a.partDrawingId =:partDrawingId ");
			args.put("partDrawingId", partDrawingId);
		}
		
		String invIOUid = (String) params.get("invIOUid");
		if (StringUtils.isNotEmpty(invIOUid)) {
			coreHql.append(" and a.invIOUid =:invIOUid ");
			args.put("invIOUid", invIOUid);
		}
		String warehouseUid = (String) params.get("warehouseUid");
		if (StringUtils.isNotEmpty(warehouseUid)) {
			coreHql.append(" and a.warehouseUid =:warehouseUid ");
			args.put("warehouseUid", warehouseUid);
		}
		String partDescription = (String) params.get("partDescription");
		if (StringUtils.isNotEmpty(partDescription)) {
			coreHql.append(" and a.partDescription =:partDescription ");
			args.put("partDescription", partDescription);
		}
				
		String batchnum = (String) params.get("batchnum");
		if (StringUtils.isNotEmpty(batchnum)) {
			coreHql.append(" and a.batchnum =:batchnum ");
			args.put("batchnum", batchnum);
		}
		
				
		String countHql = "select count(*) " + coreHql.toString();
		String hql = coreHql.toString();
				
		Map<String, Object> newArgs = new HashMap<String, Object>();
		String key;
		String newKey;
		Iterator<String> iterator = args.keySet().iterator();
		while(iterator.hasNext()){
			key = (String)iterator.next();
			newKey = key.replace(".", "_");
			newArgs.put(newKey, args.get(key));
			hql = hql.replace(":"+key, ":"+newKey);
			countHql = countHql.replace(":"+key, ":"+newKey);
					
			System.out.println("hql: " + hql);
			System.out.println("countHql: " + countHql);
			System.out.println("newArgs: " + newArgs);
		}
		this.pagingQuery(page, hql, countHql, newArgs);
	}	
}
